home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / ZPROPS.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  8KB  |  278 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zprops.c */
  20. /* Device property operators */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "alloc.h"
  24. #include "dict.h"
  25. #include "errors.h"
  26. #include "iname.h"
  27. #include "oper.h"
  28. #include "state.h"
  29. #include "store.h"
  30. #include "gsprops.h"
  31. #include "gsmatrix.h"            /* for gxdevice.h */
  32. #include "gxdevice.h"
  33. #include "gsstate.h"
  34.  
  35. /* Forward references */
  36. private int props_to_stack(P3(const gs_prop_item *, os_ptr, int));
  37. private int props_from_stack(P3(gs_prop_item *, const_os_ptr, int));
  38.  
  39. /* <device> getdeviceprops <mark> <name> <value> ... */
  40. int
  41. zgetdeviceprops(os_ptr op)
  42. {    gx_device *dev;
  43.     gs_prop_item *plist;
  44.     int count;
  45.     int code;
  46.     check_type(*op, t_device);
  47.     dev = op->value.pdevice;
  48.     count = gs_getdeviceprops_size(dev);
  49.     plist = (gs_prop_item *)alloc(count, sizeof(gs_prop_item), "getdeviceprops");
  50.     if ( plist == 0 ) return_error(e_VMerror);
  51.     code = gs_getdeviceprops(dev, plist);
  52.     if ( code >= 0 ) code = props_to_stack(plist, op + 1, count);
  53.     alloc_free((char *)plist, count, sizeof(gs_prop_item), "getdeviceprops");
  54.     if ( code >= 0 )
  55.        {    make_mark(op);
  56.         push(code);
  57.         code = 0;
  58.        }
  59.     return code;
  60. }
  61.  
  62. /* <mark> <name> <value> ... <device> putdeviceprops <device> */
  63. int
  64. zputdeviceprops(os_ptr op)
  65. {    gx_device *dev;
  66.     gs_prop_item *plist;
  67.     os_ptr mp;
  68.     int count, acount = 0;
  69.     int code;
  70.     int old_width, old_height;
  71.     check_type(*op, t_device);
  72.     dev = op->value.pdevice;
  73.     old_width = dev->width;
  74.     old_height = dev->height;
  75.     for ( mp = op - 1; !r_has_type(mp, t_mark); mp-- )
  76.        {    if ( mp <= osbot )
  77.             return_error(e_unmatchedmark);
  78.         if ( r_is_array(mp) )
  79.             acount += r_size(mp);
  80.        }
  81.     count = op - mp - 1;
  82.     if ( count & 1 )
  83.         return_error(e_rangecheck);
  84.     count >>= 1;
  85.     plist = (gs_prop_item *)alloc(count + acount, sizeof(gs_prop_item), "putdeviceprops");
  86.     if ( plist == 0 ) return_error(e_VMerror);
  87.     code = props_from_stack(plist, mp + 1, count);
  88.     if ( code >= 0 )
  89.         code = gs_putdeviceprops(dev, plist, count);
  90.     alloc_free((char *)plist, count + acount, sizeof(gs_prop_item), "putdeviceprops");
  91.     if ( code > 0 || (code == 0 && (dev->width != old_width || dev->height != old_height)) )
  92.     {    /* The device was open and is now closed, */
  93.         /* or its dimensions have changed. */
  94.         /* If it was the current device, */
  95.         /* call setdevice to reinstall it and erase the page. */
  96.         /****** DOESN'T FIND ALL THE GSTATES THAT REFERENCE THE DEVICE. ******/
  97.         if ( gs_currentdevice(igs) == dev )
  98.         {    int was_open = dev->is_open;
  99.             code = gs_setdevice(igs, dev);
  100.             /* If the device wasn't closed, */
  101.             /* setdevice won't erase the page. */
  102.             if ( was_open && code >= 0 )
  103.                 code = gs_erasepage(igs);
  104.         }
  105.     }
  106.     if ( code >= 0 )
  107.        {    *mp = *op;
  108.         osp = op = mp;
  109.         code = 0;
  110.        }
  111.     return code;
  112. }
  113.  
  114. /* ------ Initialization procedure ------ */
  115.  
  116. op_def zprops_op_defs[] = {
  117.     {"1getdeviceprops", zgetdeviceprops},
  118.     {"2putdeviceprops", zputdeviceprops},
  119.     op_def_end(0)
  120. };
  121.  
  122. /* ------ Internal routines ------ */
  123.  
  124. /* Get properties from a property list to the stack. */
  125. private int
  126. props_to_stack(const gs_prop_item *plist, os_ptr op0, int count)
  127. {    const gs_prop_item *pi;
  128.     os_ptr op;
  129.     int i;
  130.     int code;
  131.     for ( op = op0, pi = plist, i = count; i != 0; pi++, i-- )
  132.        {    ref value;
  133.         const char *nstr = pi->pname;
  134.         int nlen = pi->name_size;
  135.         if ( nstr == 0 ) continue;    /* no name, skip */
  136.         if ( ostop - op < 2 )
  137.             return_error(e_stackoverflow);
  138.         if ( nlen < 0 ) nlen = strlen(nstr);
  139.         code = name_ref((const byte *)nstr, nlen, op, 0);
  140.         if ( code < 0 ) return code;
  141.         switch ( pi->type )
  142.            {
  143.         case (int)prt_int:
  144.             make_int(&value, pi->value.i);
  145.             break;
  146.         case (int)prt_float:
  147.             make_real(&value, pi->value.f);
  148.             break;
  149.         case (int)prt_bool:
  150.             make_bool(&value, pi->value.b);
  151.             break;
  152.         case (int)prt_string:
  153.            {    ushort size = pi->value.a.size;
  154.             char *str;
  155.             if ( size == (ushort)(-1) )
  156.                 size = strlen(pi->value.a.p.s);
  157.             str = alloc(size, 1, "props_to_stack(string)");
  158.             if ( str == 0 )
  159.                 return_error(e_VMerror);
  160.             memcpy(str, pi->value.a.p.s, size);
  161.             make_string(&value, a_all, size, (byte *)str);
  162.            }    break;
  163.         case (int)prt_int_array:
  164.         case (int)prt_float_array:
  165.            {    uint size = pi->value.a.size;
  166.             ref *arefs;
  167.             uint j;
  168.             gs_prop_item *pv = pi->value.a.p.v;
  169.             code = alloc_array(&value, a_all, size, "props_to_stack(array)");
  170.             if ( code < 0 )
  171.                 return code;
  172.             arefs = value.value.refs;
  173.             for ( j = 0; j < size; j++, arefs++, pv++ )
  174.               if ( pi->type == prt_int_array )
  175.                 make_int_new(arefs, pv->value.i);
  176.               else
  177.                 make_real_new(arefs, pv->value.f);
  178.            }    break;
  179.         default:
  180.             return_error(e_typecheck);
  181.            }
  182.         ref_assign(op + 1, &value);
  183.         op += 2;
  184.        }
  185.     return op - op0;
  186. }
  187.  
  188. /* Set properties from the stack. */
  189. /* Returns the number of elements copied. */
  190. /* Entries with non-name keys are not copied; */
  191. /* entries with invalid values are copied with status = pv_typecheck. */
  192. private int
  193. props_from_stack(gs_prop_item *plist /* [count + acount] */, const_os_ptr op0,
  194.   int count)
  195. {    gs_prop_item *pi = plist;
  196.     gs_prop_item *pai = plist + count;
  197.     const_os_ptr op = op0 + 1;
  198.     for ( ; count; op += 2, count-- )
  199.        {    ref sref;
  200.         if ( !r_has_type(op - 1, t_name) )
  201.             return_error(e_typecheck);
  202.         name_string_ref(op - 1, &sref);
  203.         pi->pname = (char *)sref.value.bytes;
  204.         pi->name_size = r_size(&sref);
  205.         pi->status = pv_set;
  206.         switch ( r_type(op) )
  207.            {
  208.         case t_null:
  209.             pi->type = prt_null;
  210.             break;
  211.         case t_integer:
  212.             pi->type = prt_int;
  213.             pi->value.i = op->value.intval;
  214.             break;
  215.         case t_real:
  216.             pi->type = prt_float;
  217.             pi->value.f = op->value.realval;
  218.             break;
  219.         case t_boolean:
  220.             pi->type = prt_bool;
  221.             pi->value.b = op->value.index;
  222.             break;
  223.         case t_name:
  224.             name_string_ref(op, &sref);
  225.             goto nst;
  226.         case t_string:
  227.             ref_assign(&sref, op);
  228.             pi->type = prt_string;
  229.             pi->value.a.p.s = (char *)op->value.bytes;
  230. nst:            pi->value.a.size = r_size(&sref);
  231.             break;
  232.         case t_array:
  233.         case t_mixedarray:
  234.         case t_shortarray:
  235.            {    uint size = r_size(op);
  236.             uint i;
  237.             gs_prop_item *pv;
  238.             gs_prop_type tv = prt_int;
  239.             pi->type = prt_int_array;
  240.             pi->value.a.p.v = pai;
  241.             pi->value.a.size = size;
  242. top:            pv = pai;
  243.             for ( i = 0; i < size; i++ )
  244.                {    ref rnum;
  245.                 array_get(op, (long)i, &rnum);
  246.                 pv->pname = 0;
  247.                 pv->type = tv;
  248.                 switch ( r_type(&rnum) )
  249.                    {
  250.                 case t_real:
  251.                     if ( tv == prt_int )
  252.                        {    tv = prt_float;
  253.                         pi->type = prt_float_array;
  254.                         goto top;
  255.                        }
  256.                     pv++->value.f = rnum.value.realval;
  257.                     break;
  258.                 case t_integer:
  259.                     if ( tv == prt_int )
  260.                       pv++->value.i = rnum.value.intval;
  261.                     else
  262.                       pv++->value.f = rnum.value.intval;
  263.                     break;
  264.                 default:
  265.                     pi->status = pv_typecheck;
  266.                    }
  267.                }
  268.  
  269.             pai = pv;
  270.            }    break;
  271.         default:
  272.             pi->status = pv_typecheck;
  273.            }
  274.         pi++;
  275.        }
  276.     return 0;
  277. }
  278.